#include <algorithm>
#include<string>
#include<vector>
#include<array>
#include<unordered_map>
class Solution {
public:
    int numRescueBoats(std::vector<int>& people, int limit) {
        std::sort(people.begin(), people.end());
        int n = people.size();
        int l = 0;
        int r= n-1;
        int ans = 0;
        while (l<=r)
        {
            if (people[l]+people[r]<=limit)
            {
                ans++;
                l++;
                r--;
            }
            else
            {
                r--;
                ans++;
            }
        }
        return ans;
    }
};
